package net.shao.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import net.shao.R;


public class MyProgress extends ProgressBar {
    //文字大小
    private int mTextViewSize = sp2px(10);
    //未显示的进度
    private int unreached_color = 0xFFd3d6da;
    //文字颜色
    private int mTextViewColor = 0XFFFC00D1;
    // 显示的进度
    private int reached_color = 0XFFFC00D1;
    //未显示的进度高度
    private int unreached_height = dp2px(2);
    //显示的进度高度
    private int reached_height = dp2px(2);
    //文字与显示以及未显示进度的间隔
    private int mText_interval = dp2px(10);
    //判断是否到达100%
    private boolean flag = false;
    //初始化画笔
    private Paint paint = new Paint();
    //获得进度条的真实宽度
    private int mRealWidth;


    public MyProgress(Context context) {
        super(context, null);
    }


    public MyProgress(Context context, AttributeSet attrs) {
        super(context, attrs);

        //		setHorizontalScrollBarEnabled(true);

        obtainStyledAttributes(attrs);

        paint.setTextSize(mTextViewSize);
    }

    /**
     * 获得自定义属性值
     *
     * @param attrs
     */
    private void obtainStyledAttributes(AttributeSet attrs) {
        final TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
                R.styleable.MyProgressBar);

        mTextViewSize = (int) typedArray.getDimension(R.styleable.MyProgressBar_textSize, mTextViewSize);
        mTextViewColor = typedArray.getColor(R.styleable.MyProgressBar_textColor, mTextViewColor);
        mText_interval = (int) typedArray.getDimension(R.styleable.MyProgressBar_interval, mText_interval);

        unreached_color = typedArray.getColor(R.styleable.MyProgressBar_progress_unreached_color, unreached_color);
        unreached_height = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_unreached_bar_height, unreached_height);

        reached_color = typedArray.getColor(R.styleable.MyProgressBar_progress_reached_color, reached_color);
        reached_height = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_reached_bar_height, reached_height);

        typedArray.recycle();
    }


    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,
                                          int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        //确定View的宽高
        setMeasuredDimension(width, height);
        //得到真实宽度
        mRealWidth = getMeasuredWidth() - getPaddingTop() - getPaddingBottom();
    }

    /**
     * 根据模式设置高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        //获得View高度的模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获得View高度值
        int size = MeasureSpec.getSize(heightMeasureSpec);
        //判断View的模式
        if (heightMode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            //获得文字的高度
            int textHeight = (int) (paint.descent() - paint.ascent());
            //获得最高的高度
            result = getPaddingTop() + getPaddingBottom() + Math.max(Math.abs(textHeight),
                    Math.max(unreached_height, reached_height));
            if (heightMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        //将画布移到真实0点，以及水平位置
        canvas.translate(getPaddingLeft(), getHeight() / 2);
        //获得文本内容
        String text = getProgress() + "%";
        float textWidth = paint.measureText(text);
        //获得当前进度
        float radio = getProgress() * 1.0f / getMax();
        float progressX = radio * mRealWidth;

        if (progressX + textWidth > mRealWidth) {
            progressX = mRealWidth - textWidth;
            flag = true;
        }

        //获得当前进度的最后的位置
        float endX = progressX - mText_interval / 2;

        if (endX > 0) {
//			if(endX+textWidth>=mRealWidth){
//				paint.setColor(reached_color);
//				paint.setStrokeWidth(reached_height);
//				canvas.drawLine(0, 0, progressX-mText_interval/2, 0, paint);
//			}else{
            paint.setColor(reached_color);
            paint.setStrokeWidth(reached_height);
            canvas.drawLine(0, 0, endX, 0, paint);
//			}
        }
        paint.setColor(mTextViewColor);
        float textHeight = (paint.descent() + paint.ascent()) / 2;
        //		 int y=(int) ((paint.descent()-paint.ascent())/2);
        canvas.drawText(text, progressX, -textHeight, paint);
        if (flag == false) {
            float start = progressX + textWidth + mText_interval / 2;
            paint.setColor(unreached_color);
            paint.setStrokeWidth(unreached_height);
            canvas.drawLine(start, 0, mRealWidth, 0, paint);
        }
        canvas.restore();
    }


    /**
     * sp转换成px
     *
     * @param size
     * @return
     */
    private int sp2px(int size) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                size, getResources().getDisplayMetrics());
    }

    /**
     * dp转换成px
     *
     * @param size
     * @return
     */
    private int dp2px(int size) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                size, getResources().getDisplayMetrics());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRealWidth = w - getPaddingRight() - getPaddingLeft();
    }

}
